home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_02 / otto / freq.c < prev   
C/C++ Source or Header  |  1993-12-01  |  1KB  |  57 lines

  1. /********************************************
  2.  * Generate a header file with freqency
  3.  * distribution obtained from specified
  4.  * file
  5.  *
  6.  * 1993 by Erick Otto
  7.  ********************************************/
  8. #include <stdio.h>
  9.  
  10. main(argc,argv)
  11. int argc;
  12. char *argv[];
  13. {
  14.  
  15.     FILE *iptr,*optr;
  16.     int dist[256];
  17.     int c;
  18.     int i;
  19.     int total;
  20.  
  21.     for (i=0;i<256;i++) dist[i] =0;
  22.     total=0;
  23.  
  24.     if (argc < 3) {
  25.     fprintf(stderr,"Not enough arguments\n");
  26.     fprintf(stderr,"%s infile outfile\n",argv[0]);
  27.     exit(1);
  28.     }
  29.  
  30.     if ((iptr = fopen(argv[1],"r")) == NULL) {
  31.         fprintf(stderr,
  32.            "Can not open file %s\n",argv[1]);
  33.     }
  34.     if ((optr = fopen(argv[2],"w")) == NULL) {
  35.         fprintf(stderr,
  36.            "Can not open file %s\n",argv[2]);
  37.     }
  38.  
  39.     while ((c = getc(iptr)) != EOF) {
  40.         total++;
  41.         dist[c]++;
  42.     }
  43.  
  44.     fprintf(optr,"double freq[256] = { /* From %s */\n",
  45.        argv[1]);
  46.  
  47.     for (i=0;i<256;i++) {
  48.  
  49.         fprintf(optr,"%G, ",(double)dist[i]/(double)total);
  50.  
  51.     if (i !=0 && (i % 4) == 0)  fprintf(optr,"\n");
  52.     }
  53.     fprintf(optr,"};\n");
  54. }
  55.  
  56.  
  57.